package com.heima.behavior.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.检查参数  参数和登陆状态
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //2.获取点赞的行为信息，判断是否已经点过赞，已经点过赞，则不能重复点赞
        //获取用户行为实体类
        ApBehaviorEntry byUserIdOrEquipmentId = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (byUserIdOrEquipmentId==null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"获取行为实体信息失败");
        }
        Query query = Query.query(Criteria.where("entryId").is(byUserIdOrEquipmentId.getId()).and("articleId").is(dto.getArticleId()));
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (dto.getOperation()==0&&apLikesBehavior!=null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"您已经点过赞，请勿重复操作");
        }
        //3.保存点赞的行为信息
        if (dto.getOperation() == 0) {
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setEntryId(byUserIdOrEquipmentId.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType((short) 0);
            apLikesBehavior.setOperation((short) 0);
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior);
        } else {
            mongoTemplate.remove(query,ApLikesBehavior.class);
        }
        //发送点赞行为给Kafka stream 处理
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(dto.getOperation()==0?1:-1);
        kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(mess));
        log.info("点赞行为 发送kafka消息 ==>{}",JSON.toJSONString(mess));
        return ResponseResult.okResult();
    }
}
